import Vue from 'vue'
import Vuex from 'vuex'
import store from '../store'
import Utils from './utils'
let vue = Vue.use(Vuex)
let utils = new Utils()
export default class personUtils {

    constructor(options) {
    }

    hasRole(role){
        return (store.state.user.role.roles & role) == role
    }

    getApproveData(owner, user) {
        if(this.hasRole(4)){
            return {
                approver:'',
                reason:''
            }
        }
        var approveData = owner.$refs.approveReq.getData()
        if (!approveData.approval || !approveData.reason) {
            return null
        }
        return approveData
    }

    buildDbData(approveData,userData) {
        var positions = ''
        for (var pos in userData.userInfo.job.positions) {
          if (!positions) {
            positions = userData.userInfo.job.positions[pos]
          }
          else
            positions = positions + ',' + userData.userInfo.job.positions[pos]
        }
    
        var dbData = {
          annualAssessment: userData.kpis,
          projects: userData.projects,
          takePart: userData.races,
          technicalPosition: userData.professionTechs,
          continueTrain: userData.parttimeEdus,
          onlineWork: userData.trainings,
          socialReltaion: userData.relations,
          otherInfo: userData.extrainfo,
          resume: userData.userInfo.resumes,
          workerType: userData.userInfo.profile.workType,
          image:userData.userInfo.profile.image,
          state: userData.userInfo.profile.empState,
          name: userData.userInfo.profile.name,
          sex: userData.userInfo.profile.sex,
          idCard: userData.userInfo.profile.cert,
          nation: userData.userInfo.profile.national,
          joinWorkTime: this.decorateDate(userData.userInfo.profile.joinWorkTime),
          birthday: this.decorateDate(userData.userInfo.profile.birthday),
          nativePlace: userData.userInfo.profile.birthplace.province + ',' + userData.userInfo.profile.birthplace.city,
          politicalStatus: userData.userInfo.profile.policy,
          joinPartyTime: this.decorateDate(userData.userInfo.profile.ccptime),
          createTime: this.decorateDate(userData.userInfo.employsource.enterTime),
          source: userData.userInfo.employsource.channel,
          departmentId: userData.userInfo.employsource.depart[userData.userInfo.employsource.depart.length - 1],
          FtEducation: userData.userInfo.education.ftDegree,
          FtSchool: userData.userInfo.education.ftDiscipline,
          FtGraduateTime:userData.userInfo.education.FtGraduateTime,
          PtEducation: userData.userInfo.education.ptDegree,
          PtSchool: userData.userInfo.education.ptDiscipline,
          PtGraduateTime:userData.userInfo.education.PtGraduateTime,
          job: userData.userInfo.job.positionlevel,
          gwType: userData.userInfo.job.postType,
          positions: positions,
          positionType:userData.userInfo.job.positionType,
          tel: userData.extrainfo.extrabaseinfo.tel,
          phone: userData.extrainfo.extrabaseinfo.phone,
          baseInfo: userData.userInfo,
          extraInfo: userData.extrainfo,
          married: userData.extrainfo.extrabaseinfo.married
        }
        dbData.FtGraduateTime = this.decorateDate(dbData.FtGraduateTime)
        dbData.PtGraduateTime = this.decorateDate(dbData.PtGraduateTime)
        dbData.otherInfo.extrabaseinfo.firstsigntime = this.decorateDate(dbData.otherInfo.extrabaseinfo.firstsigntime)
        dbData.otherInfo.extrabaseinfo.lastsigntime = this.decorateDate(dbData.otherInfo.extrabaseinfo.lastsigntime)
        dbData.otherInfo.extrabaseinfo.retiretime = this.decorateDate(dbData.otherInfo.extrabaseinfo.retiretime)
        dbData.otherInfo.extrabaseinfo.fistengagetime = this.decorateDate(dbData.otherInfo.extrabaseinfo.fistengagetime)
        dbData.otherInfo.extrabaseinfo.engageLastTime = this.decorateDate(dbData.otherInfo.extrabaseinfo.engageLastTime)
        if (userData.id) {
          dbData.id = userData.id
        }
    
        return {
          approval:approveData.approval,
          reason:approveData.reason,
          person:dbData
        }
    }

    decorateDate(time){
        if(time){
            if(time.split('-').length == 2){
              return time + '-01'
            }
        }
        return time
    }
    updateUser(owner, user, sucCallback, failedCallback) {
        var approveData = this.getApproveData(owner, user)
        if (approveData) {
            var postData = this.buildDbData(approveData,user)
            console.log("update POST json:"+JSON.stringify(postData))
            vue.http.post('/hr/person/update', JSON.stringify(postData)).then(response => {
                console.log("update person response" + response);
                if(sucCallback)
                    sucCallback(response)
            }, response => {
                if(failedCallback)
                    failedCallback(response)
            })
        }
        else {
            owner.showErrorInfo("请填写提审信息!")
        }
    }

    exportContractdata(filename){
        var queryData = {
            type:3 //通讯录导出
        }
        var fName = filename
        if(filename.indexOf(".xls") <= 0 ){
            fName = filename+".xls"
        }
        var url =encodeURI('/hr/person/export?filename='+fName+"&query="+encodeURI(JSON.stringify(queryData))+"&user="+store.state.user.id+"&role="+store.state.user.role.id+"&type=3")
        window.open(url)
    }

    exportData(queryData,filename,type){
        if(!type){
            type = 5
        }
        //queryData.type = 5 //人事
        var fName = filename
        if(filename.indexOf(".xls") <= 0 ){
            fName = filename+".xls"
        }
        var url =encodeURI('/hr/person/export?filename='+fName+"&query="+encodeURI(JSON.stringify(queryData))
                    +"&user="+store.state.user.id+"&role="+store.state.user.role.id+"&type="+type)
        window.open(url)
    }

    dispatch(owner, user, sucCallback, failedCallback) {
        if (owner.getOperation() == 'delete') {
            return this.deleteUser(owner, user, sucCallback, failedCallback)
        }
        else if (owner.getOperation() == 'update') {
            return this.updateUser(owner, user, sucCallback, failedCallback)
        }
    }

    deleteUser(owner, user, sucCallback, failedCallback) {
        var approveData = this.getApproveData(owner, user)
        if (approveData) {
            var postData = {
                approval: approveData.approval,
                reason: approveData.reason,
                id: user.id
            }
            vue.http.delete('/hr/person/delete',{body:postData}).then(response => {
                console.log("delete person response" + response);
                if(sucCallback)
                    sucCallback(response)
            }, response => {
                if(failedCallback)
                    failedCallback(response)
            })
        }
        else {
            owner.showErrorInfo("请填写提审信息!")
        }
    }
}